<?php

namespace mobile\controllers;

use Yii;

use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;
use yii\base\ErrorException;
use yii\helpers\Html;
use common\models\member\Member;
use common\models\notice\Notice;
use common\models\notice\NoticeSearch;
use yii\web\UploadedFile;
use zhongkefu\zhongkefu;
use zhongkefu\helpers\Utils;
use frontend\models\FenhuiForm;
use yii\data\Pagination;

class MemberController extends Controller
{
    public $layout = 'member';
    //会员基础信息显示
    public function actionInfo(){
        $parenttitle = '会员中心';
        $view = Yii::$app->view;
        $view->params['title'] = $parenttitle;

        $view = Yii::$app->view;
        $view->params['title'] = '个人中心'; 

        $member =  Member::findOne(['id'=>\Yii::$app->user->identity->getId()]);
        $yyzhizhaoimg = json_decode($member->yyzhizhaoimg, true);
        $fwdengjiimg = json_decode($member->fwdengjiimg, true);
        $cardz = json_decode($member->cardz, true);
        $cardf = json_decode($member->cardf, true);
        return $this->render('info',[
            'member'=>$member,
            'yyzhizhaoimg' =>$yyzhizhaoimg,
            'fwdengjiimg' =>$fwdengjiimg,
            'cardz' =>$cardz,
            'cardf' =>$cardf,

        ]);
    }
    //更新会员基础信息
    public function actionUpdateinfo(){
        $ucenter = Member::findOne(Yii::$app->user->identity->getId());
        if(Yii::$app->request->post()){
            if(!empty(Yii::$app->request->post()['repassword']))
            {
                if(Yii::$app->security->validatePassword(Yii::$app->request->post()['oldpassword'], $ucenter->password_hash))
                {
                    $password = Yii::$app->request->post()['repassword'];
                    $ucenter->setPassword($password);
                    $ucenter->generateAuthKey();
                    $ucenter->save(false);
                    if($ucenter->save(false)){
                        \Yii::$app->getSession()->setFlash("success", '修改成功');
                        return $this->redirect(['/auth/logout']);
                    }
                    else
                    {
                         Yii::$app->session->setFlash('success', Yii::t('app', '修改失败！！'));
                    }
                }
                else{
                    Yii::$app->session->setFlash('success', Yii::t('app', '原密码输入错误！！'));
                    return $this->render('info', [
                        'model' => $ucenter,
                        'member'=>$ucenter,
                    ]);
                }
            }
            else
            {
                $ucenter->company = Yii::$app->request->post()['company'];
                if(!empty(Yii::$app->request->post()['loc_province'])){
                    $ucenter->loc_province = Yii::$app->request->post()['loc_province'];
                }
                if(!empty(Yii::$app->request->post()['loc_city'])){
                    $ucenter->loc_city = Yii::$app->request->post()['loc_city'];
                }
                if(!empty(Yii::$app->request->post()['loc_town'])){
                    $ucenter->loc_town = Yii::$app->request->post()['loc_town'];
                }
                $ucenter->address = Yii::$app->request->post()['address'];
                $ucenter->farentel = Yii::$app->request->post()['farentel'];
                $ucenter->grnum = Yii::$app->request->post()['grnum'];
                $ucenter->zhucezj = Yii::$app->request->post()['zhucezj'];
                $ucenter->faren = Yii::$app->request->post()['faren'];
                $ucenter->farenphone = Yii::$app->request->post()['farenphone'];
                $ucenter->farenemail = Yii::$app->request->post()['farenemail'];
                $ucenter->yyzhizao = Yii::$app->request->post()['yyzhizao'];
                $ucenter->zcdizhi = Yii::$app->request->post()['zcdizhi'];
                $ucenter->qychengli = Yii::$app->request->post()['qychengli'];
                $ucenter->qychenglizhi = Yii::$app->request->post()['qychenglizhi'];
                $ucenter->jyfanwei = Yii::$app->request->post()['jyfanwei'];
                $ucenter->card = Yii::$app->request->post()['card'];
                $siteRoot = Yii::$app->params['memberimg'];
                if(!empty($_FILES['yyzhizhaoimg']['name'])){
                    
                    $json1 = Utils::uploadfile($_FILES['yyzhizhaoimg']['tmp_name'],$_FILES['yyzhizhaoimg']['name'],$siteRoot);
                    $ucenter->yyzhizhaoimg = $json1;
                }
                $ucenter->faren = Yii::$app->request->post()['faren'];
               if(!empty($_FILES['cardz']['name'])){
                    
                    $json1 = Utils::uploadfile($_FILES['cardz']['tmp_name'],$_FILES['cardz']['name'],$siteRoot);
                    $ucenter->cardz = $json1;
                }
                if(!empty($_FILES['cardf']['name'])){
                    $json2 = Utils::uploadfile($_FILES['cardf']['tmp_name'],$_FILES['cardf']['name'],$siteRoot);
                    $ucenter->cardf = $json2;
                }
                if(!empty($_FILES['fwdengjiimg']['name'])){
                    $json3 = Utils::uploadfile($_FILES['fwdengjiimg']['tmp_name'],$_FILES['fwdengjiimg']['name'],$siteRoot);
                    $ucenter->fwdengjiimg = $json3;
                }
                if($ucenter->save(false))
                {
                    \Yii::$app->getSession()->setFlash("success", '修改成功');
                    return $this->redirect(['member/info']);
                }
            } 

            }
            
    }
    //我的消息列表页
    public function actionMessage(){
        $parenttitle = '我的消息';
        $view = Yii::$app->view;
        $view->params['title'] = $parenttitle;
        $notice = Notice::find();
        $pages = new Pagination(['totalCount'=>$notice->count(), 'pageSize'=>'12']);  //分页
        //动态读取内容
        $datas = $notice->offset($pages->offset)
        ->limit($pages->limit)
        ->orderby('id DESC')
        ->all();
        $size=12;
        if(isset($_GET['page'])){
            $page=$_GET['page'];
            $perp=$size;
        }else{
            $page=1;
            $perp=$size;
        }
        $num =$notice->count();
        $cpage=ceil($num/$size);
            return $this->render('message',['list' => $datas,'size' => $size,'perp' => $perp,'page' => $page,'cpage' => $cpage,'pages' => $pages,'count'=>$notice->count()]);

    }
    //我的消息内页
    public function actionMessagepage($id){
        $parenttitle = '我的消息';
        $view = Yii::$app->view;
        $view->params['title'] = $parenttitle;
        $message = Notice::find()->where(['id' => $id])->asArray()->one();
        $articles = Notice::find()->asArray()->all();
        foreach($articles as $k=>$t){
            if($t['id'] == $id){
                //$lastone = $t[$k-1]['id'];
                $last = $k - 1;
                $next = $k + 1;
                //$nextone = $t[($k+1)]['id'];
          }
      }

        if(isset($articles[$last])) {
            $lastone['id'] = $articles[$last]['id'];
            $lastone['name'] = $articles[$last]['title'];
        } else {
            $lastone['id'] = $id;
            $lastone['name'] = '没有了';
        }

        if(isset($articles[$next])) {
            $nextone['id'] = $articles[$next]['id'];
            $nextone['name'] = $articles[$next]['title'];
        } else {
            $nextone['id'] = $id;
            $nextone['name'] = '没有了';
        }
         return $this->render('messagepage',[
            'message'=>$message,
            'lastone' => $lastone,
            'nextone' => $nextone,
        ]);
        //$lastone =
        //$lastone = 
       
    }
    //开通会员
    public function actionOpenmember(){
        $member =  Member::findOne(['id'=>\Yii::$app->user->identity->getId()]);
        return $this->render('openmember',[
            'member'=>$member
        ]);
    }

    //显示密码页
    public function actionPwd(){
        return $this->render('pwd');
    }
	//修改密码
	public function actionUpdate()
    {
        $model = Member::findOne(['id'=>\Yii::$app->user->identity->getId()]);

        if(Yii::$app->request->post()){
            if(Yii::$app->security->validatePassword(Yii::$app->request->post()['oldpassword'], $model->password_hash)){
				$password = Yii::$app->request->post()['repassword'];
				$model->setPassword($password);
				$model->generateAuthKey();
				if($model->save()){
					Yii::$app->session->setFlash('success', '密码修改成功！');
					return $this->redirect(['/auth/logout']);
                }else{
					Yii::$app->session->setFlash('success', "密码修改失败！");
					return $this->redirect(['/member/info']);
				}
            }else{
                Yii::$app->session->setFlash('success', Yii::t('app', '原密码输入错误！！'));
                return $this->render('pwd', [
                    'model' => $model,
                ]);
            }

        } else {
            return $this->render('pwd', [
                'model' => $model,
            ]);
        }
    }

    //通知消息
    public function actionNotice(){
        $notice = Notice::find()->all();
        return $this->render('notice',[
            'notice'=>$notice
        ]);
    }
    //通知消息详情
    public function actionNoticedetail(){
        $id = Yii::$app->request->get()['id'];
        $notice = Notice::find()->andWhere(['id'=>$id])->one();
        return $this->render('noticedetail',[
            'notice'=>$notice
        ]);
    }
    //营业执照
    public function actionLicence(){
        $member =  Member::findOne(['id'=>\Yii::$app->user->identity->getId()]);
         return $this->render('licence',[
            'member'=>$member
        ]);
    }
    //更新营业执照
    public function actionUpdatelicence(){
        $member =  Member::findOne(['id'=>\Yii::$app->user->identity->getId()]);
         $member->yyzhizao = Yii::$app->request->post()['yyzhizao'];
         $member->zzdengji = Yii::$app->request->post()['zzdengji'];
         $member->qychengli = Yii::$app->request->post()['qychengli'];
         $member->zjjigou = Yii::$app->request->post()['zjjigou'];
         $member->faren = Yii::$app->request->post()['faren'];
         $member->farentel = Yii::$app->request->post()['farentel'];
         $member->farensex = Yii::$app->request->post()['farensex'];
         $member->farenphone = Yii::$app->request->post()['farenphone'];
         $member->farenemail = Yii::$app->request->post()['farenemail'];
         $member->zhucezj = Yii::$app->request->post()['zhucezj'];
         $member->zcdizhi = Yii::$app->request->post()['zcdizhi'];
         $member->zcyoubian = Yii::$app->request->post()['zcyoubian'];
         $member->jyfanwei = Yii::$app->request->post()['jyfanwei'];
         $member->frdaibiaojj = Yii::$app->request->post()['frdaibiaojj'];

        $siteRoot = Yii::$app->params['memberimg'];
           if(!empty($_FILES['yyzhizhaoimg']['name'])){
                
                $json1 = Utils::uploadfile($_FILES['yyzhizhaoimg']['tmp_name'],$_FILES['yyzhizhaoimg']['name'],$siteRoot);
                $member->yyzhizhaoimg = $json1;
            }
            if(!empty($_FILES['fwdengjiimg']['name'])){
                $json2 = Utils::uploadfile($_FILES['fwdengjiimg']['tmp_name'],$_FILES['fwdengjiimg']['name'],$siteRoot);
                $member->fwdengjiimg = $json2;
            }
       
             if(!empty($_FILES['zzjigouimg']['name'])){
                $json3 = Utils::uploadfile($_FILES['zzjigouimg']['tmp_name'],$_FILES['zzjigouimg']['name'],$siteRoot);
                $member->zzjigouimg = $json3;
            }
        
         if($member->save(false)){
              return $this->redirect(['member/licence']);
         }
    }
    //企业资料
    public function actionBusdetail(){
         $member =  Member::findOne(['id'=>\Yii::$app->user->identity->getId()]);
         return $this->render('busdetail',[
            'member'=>$member
        ]);
    }
    //更新企业资料
    public function actionUpdatebusdetail(){
         $member =  Member::findOne(['id'=>\Yii::$app->user->identity->getId()]);
         $member->xingzhi = Yii::$app->request->post()['xingzhi'];
         $member->moshi = Yii::$app->request->post()['moshi'];
         $member->pinpai = Yii::$app->request->post()['pinpai'];
         $member->daipinpai = Yii::$app->request->post()['daipinpai'];
         $member->bennum = Yii::$app->request->post()['bennum'];
         $member->grnum = Yii::$app->request->post()['grnum'];
         $member->nianmoney = Yii::$app->request->post()['nianmoney'];
         $member->wairatio = Yii::$app->request->post()['wairatio'];
         $member->caigoutoubiao = Yii::$app->request->post()['caigoutoubiao'];
         if($member->save(false)){
              return $this->redirect(['member/busdetail']);
         }
    }
	//实名认证
	public function actionNameauth(){
		$member =  Member::findOne(['id'=>\Yii::$app->user->identity->getId()]);
         return $this->render('nameauth',[
            'member'=>$member
        ]);
	}
	
	//更新实名认证
    public function actionUpdatenameauth(){
        $member =  Member::findOne(['id'=>\Yii::$app->user->identity->getId()]);
         $member->realname = Yii::$app->request->post()['realname'];
         $member->card = Yii::$app->request->post()['card'];

        $siteRoot = Yii::$app->params['memberimg'];
           if(!empty($_FILES['cardz']['name'])){
                
                $json1 = Utils::uploadfile($_FILES['cardz']['tmp_name'],$_FILES['cardz']['name'],$siteRoot);
                $member->cardz = $json1;
            }
            if(!empty($_FILES['cardf']['name'])){
                $json2 = Utils::uploadfile($_FILES['cardf']['tmp_name'],$_FILES['cardf']['name'],$siteRoot);
                $member->cardf = $json2;
            }

        
         if($member->save(false)){
			 alert("提交成功！");
             return $this->redirect(['member/nameauth']);
         }
    }
	//认证资料管理
	public function actionAuthmange(){
		$member =  Member::findOne(['id'=>\Yii::$app->user->identity->getId()]);
		$cardz = json_decode($member->cardz, true);
		$cardf = json_decode($member->cardf, true);
         return $this->render('authmange',[
            'member'=>$member,
			'cardz'=>$cardz,
			'cardf'=>$cardf
        ]);		
	}
    //申请入会 
    public function actionApply(){
        $parenttitle = '申请入会';
        $view = Yii::$app->view;
        $view->params['title'] = $parenttitle;
        $member = Member::findOne(Yii::$app->user->identity->getId());


            //显示申请信息

            $yyzhizhaoimg = json_decode($member->yyzhizhaoimg, true);
            $fwdengjiimg = json_decode($member->fwdengjiimg, true);
            $cardz = json_decode($member->cardz, true);
            $cardf = json_decode($member->cardf, true);

            return $this->render('apply',['member'=>$member,'yyzhizhaoimg' =>$yyzhizhaoimg,
                'fwdengjiimg' =>$fwdengjiimg,
                'cardz' =>$cardz,
                'cardf' =>$cardf]);

    }
    public function actionApply_association(){
        $parenttitle = '申请入会';
        $view = Yii::$app->view;
        $view->params['title'] = $parenttitle;
        //显示申请信息
        $member = Member::findOne(Yii::$app->user->identity->getId());
        $yyzhizhaoimg = json_decode($member->yyzhizhaoimg, true);
        $fwdengjiimg = json_decode($member->fwdengjiimg, true);
        $cardz = json_decode($member->cardz, true);
        $cardf = json_decode($member->cardf, true);
        //填写申请信息
        if($_POST){
            $ucenter = Member::findOne(Yii::$app->user->identity->getId());
            if(Yii::$app->request->post()['step'] == 1){
                $ucenter->company = Yii::$app->request->post()['company'];
                if(!empty(Yii::$app->request->post()['loc_province'])){
                    $ucenter->loc_province = Yii::$app->request->post()['loc_province'];
                }
                if(!empty(Yii::$app->request->post()['loc_city'])){
                    $ucenter->loc_city = Yii::$app->request->post()['loc_city'];
                }
                if(!empty(Yii::$app->request->post()['loc_town'])){
                    $ucenter->loc_town = Yii::$app->request->post()['loc_town'];
                }
                $ucenter->address = Yii::$app->request->post()['address'];
                $ucenter->farentel = Yii::$app->request->post()['farentel'];
                $ucenter->grnum = Yii::$app->request->post()['grnum'];
                $ucenter->zhucezj = Yii::$app->request->post()['zhucezj'];
                $ucenter->faren = Yii::$app->request->post()['faren'];
                $ucenter->farenphone = Yii::$app->request->post()['farenphone'];
                $ucenter->farenemail = Yii::$app->request->post()['farenemail'];
                if($ucenter->save(false))
                {
                    \Yii::$app->getSession()->setFlash("success", '修改成功');
                    return $this->render('apply_association_step2',['member'=>$member,'yyzhizhaoimg' =>$yyzhizhaoimg,
                        'fwdengjiimg' =>$fwdengjiimg,
                        'cardz' =>$cardz,
                        'cardf' =>$cardf]);
                }
            }
            if(Yii::$app->request->post()['step'] == 2){

                $ucenter->yyzhizao = Yii::$app->request->post()['yyzhizao'];
                $ucenter->zcdizhi = Yii::$app->request->post()['zcdizhi'];
                $ucenter->qychengli = Yii::$app->request->post()['qychengli'];
                $ucenter->qychenglizhi = Yii::$app->request->post()['qychenglizhi'];
                $ucenter->jyfanwei = Yii::$app->request->post()['jyfanwei'];
                $siteRoot = Yii::$app->params['memberimg'];
                if(!empty($_FILES['yyzhizhaoimg']['name'])){

                    $json1 = Utils::uploadfile($_FILES['yyzhizhaoimg']['tmp_name'],$_FILES['yyzhizhaoimg']['name'],$siteRoot);
                    $ucenter->yyzhizhaoimg = $json1;
                }
                if($ucenter->save(false))
                {
                    \Yii::$app->getSession()->setFlash("success", '修改成功');
                    return $this->render('apply_association_step3',['member'=>$member,'yyzhizhaoimg' =>$yyzhizhaoimg,
                        'fwdengjiimg' =>$fwdengjiimg,
                        'cardz' =>$cardz,
                        'cardf' =>$cardf]);
                }

            }
            if(Yii::$app->request->post()['step'] == 3){
                $ucenter->card = Yii::$app->request->post()['card'];
                $siteRoot = Yii::$app->params['memberimg'];
                if(!empty($_FILES['cardz']['name'])){

                    $json1 = Utils::uploadfile($_FILES['cardz']['tmp_name'],$_FILES['cardz']['name'],$siteRoot);
                    $ucenter->cardz = $json1;
                }
                if(!empty($_FILES['cardf']['name'])){
                    $json2 = Utils::uploadfile($_FILES['cardf']['tmp_name'],$_FILES['cardf']['name'],$siteRoot);
                    $ucenter->cardf = $json2;
                }
                if($ucenter->save(false))
                {
                    \Yii::$app->getSession()->setFlash("success", '修改成功');
                    return $this->render('apply_association_step4',['member'=>$member,'yyzhizhaoimg' =>$yyzhizhaoimg,
                        'fwdengjiimg' =>$fwdengjiimg,
                        'cardz' =>$cardz,
                        'cardf' =>$cardf]);
                }

            }
            if(Yii::$app->request->post()['step'] == 4) {
                $siteRoot = Yii::$app->params['memberimg'];
                $ucenter->status = Yii::$app->request->post()['status'];
                if (!empty($_FILES['fwdengjiimg']['name'])) {
                    $json3 = Utils::uploadfile($_FILES['fwdengjiimg']['tmp_name'], $_FILES['fwdengjiimg']['name'], $siteRoot);
                    $ucenter->fwdengjiimg = $json3;
                }
                if ($ucenter->save(false)) {
                    \Yii::$app->getSession()->setFlash("success", '修改成功');
                    return $this->render('apply_association_step5',['member'=>$member,'yyzhizhaoimg' =>$yyzhizhaoimg,
                        'fwdengjiimg' =>$fwdengjiimg,
                        'cardz' =>$cardz,
                        'cardf' =>$cardf]);
                }
            }

        }else if($_GET){
            if(Yii::$app->request->get()['step'] == 1){
                return $this->render('apply_association',['member'=>$member,'yyzhizhaoimg' =>$yyzhizhaoimg,
                    'fwdengjiimg' =>$fwdengjiimg,
                    'cardz' =>$cardz,
                    'cardf' =>$cardf]);
            }
            if(Yii::$app->request->get()['step'] == 2){
                return $this->render('apply_association_step2',['member'=>$member,'yyzhizhaoimg' =>$yyzhizhaoimg,
                    'fwdengjiimg' =>$fwdengjiimg,
                    'cardz' =>$cardz,
                    'cardf' =>$cardf]);
            }
            if(Yii::$app->request->get()['step'] == 3){
                return $this->render('apply_association_step3',['member'=>$member,'yyzhizhaoimg' =>$yyzhizhaoimg,
                    'fwdengjiimg' =>$fwdengjiimg,
                    'cardz' =>$cardz,
                    'cardf' =>$cardf]);
            }

        }
        //显示申请信息
        $member = Member::findOne(Yii::$app->user->identity->getId());
        $yyzhizhaoimg = json_decode($member->yyzhizhaoimg, true);
        $fwdengjiimg = json_decode($member->fwdengjiimg, true);
        $cardz = json_decode($member->cardz, true);
        $cardf = json_decode($member->cardf, true);
        return $this->render('apply_association',['member'=>$member,'yyzhizhaoimg' =>$yyzhizhaoimg,
            'fwdengjiimg' =>$fwdengjiimg,
            'cardz' =>$cardz,
            'cardf' =>$cardf]);
	}
    //打印申请表 zsn 20161117
    public function actionPrint_application(){
        $userinfo = Member::findOne(['id'=>\Yii::$app->user->identity->getId()]);
        $member = FenhuiForm::findOne(['userid'=>\Yii::$app->user->identity->getId()]);
        return $this->render('print_application',['member'=>$member,'userinfo'=>$userinfo]);
    }
}